<?php

/*
 * 定时任务全部放在这里
 * */

namespace app\user\controller;


use app\common\model\AdPlayHistory;
use app\common\model\Ecpm;
use app\common\model\UserIncome;
use think\Console;
use think\Controller;
use think\Db;

class Task extends Controller
{

    //计算可提现金额
    public function calcCanTakeOutMoney()
    {


    }

    //计算每天的收益
    public function calcIncome()
    {
        //昨天的开始时间戳和结束时间戳
        $yesterday = yesterday();

        $yesterday_ecpm = Ecpm::yesterdayEcpm();


        //计算所有用户昨天的收益
        $ad_play_history = AdPlayHistory::alias('ad')->where([
            ['ad.add_time', 'BETWEEN', $yesterday]
        ])
            ->join('user user','ad.promote_user_id=user.id')
            ->field('promote_user_id,count(promote_user_id) as play_count,user.vip,user.lv_1_id,user.lv_2_id')
            ->group('promote_user_id')
            ->select();


        //计算每个用户的激励视频收益，并且插入数据库
        $user_income = [];

        foreach ($ad_play_history as $key => $value) {

            //扣除前的总收益为 ecpm/1000 * 完整播放次数   结果四舍五入，保留两位小数
            $money = round($yesterday_ecpm / 1000 * $value['play_count'], 2);

            //流量主提成
            if($value['vip']){
                $pro=0.9;
            }else{
                $pro=0.8;
            }

            //金额不足以分给上级和上上级的时候，直接全部加在自己的账户里面
            if($money>0.01 && $money<0.34){
                //所有用户的收益记录
                array_push($user_income, [
                    'user_id' => $value['promote_user_id'],
                    'lv_1_id'=>$value['lv_1_id'],
                    'lv_2_id'=>$value['lv_2_id'],
                    'money' =>$money,
                    'lv_1_income'=>0,
                    'lv_2_income'=>0,
                    'views' => $value['play_count'],
                    'ecpm' => $yesterday_ecpm,
                    'add_time' => time()
                ]);
            }else if($money>=0.34){
                //金额足够分的时候，分给上级和上上级

                //所有用户的激励视频收益记录
                array_push($user_income, [
                    'user_id' => $value['promote_user_id'],
                    'lv_1_id'=>$value['lv_1_id'],
                    'lv_2_id'=>$value['lv_2_id'],
                    'money' =>round( $money*$pro,2),
                    'lv_1_income'=>round( $money*0.07,2),
                    'lv_2_income'=>round( $money*0.03,2),
                    'views' => $value['play_count'],
                    'ecpm' => $yesterday_ecpm,
                    'add_time' => time()
                ]);

                //对流量主的金额进行更新
                Db::table('user')
                    ->where('id', $value['promote_user_id'])
                    ->inc('money', round( $money*$pro,2))
                    ->inc('lv_1_income', round($money*0.07,2))
                    ->inc('lv_2_income', round($money*0.03,2))->update();

                //上级不为0的时候，才有上上级
                if($value['lv_1_id']){
                    //给流量主上级加钱
                    Db::table('user')
                        ->where('id', $value['lv_1_id'])
                        ->setInc('money', round($money*0.07,2)) ;

                    if($value['lv_2_id']){
                        //给流量主上上级加钱
                        Db::table('user')
                            ->where('id', $value['lv_2_id'])
                            ->setInc('money', round($money*0.03,2)) ;
                    }
                }
            }else{
                //总共的金额不足一分钱，不做任何处理
            }
        }

        //为了防止重复计算，对昨天的进行删除操作，再插入
        $res2 = Db::name('user_income')->where([
            ['add_time', 'BETWEEN', today()]
        ])->delete();

        //流量主收益插入数据库
        $res = Db::name('user_income')->data($user_income)->insertAll();

    }

}
